/*
gobanjs - html5 SGF viewer for the Game of Go 

Copyright (C) 2013  Daniel Cioata
Visit http://www.govortex.ro/ for documentation, updates and examples.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/    

"use strict"; 

function Parser(gametree)
{      
    this.index=0;     
    this.gametree=gametree;
}


Parser.prototype.decodeSGFNode=function(str)
{
    var i, j, arr, p_id, p_info, props,nodeinfo;
    
    arr=str.match(/[A-Z]+(\[(.|\s)*?\])+/g)        
   
    if(arr!=null)
    {
        nodeinfo=new NodeInfo();
        for(i=0;i<arr.length;i++)
        {
            props=arr[i].split("[");            
            p_id=props[0];
            for(j=1;j<props.length;j++) 
            {                      
                //cut last ']'
                p_info=props[j].substring(0,props[j].length-1);
                /*console.log("--", p_id, p_info);*/
                nodeinfo.addInfo(p_id,p_info,this.gametree);
            }
        }        
    }    
    return nodeinfo;    
}

Parser.prototype.parseSGFNode=function(rawsgf)
{
    var j,ch,sb,str,start;
    sb=0;
    str="";
    
    start=this.index;        
    for(j=start;j<rawsgf.length;j++,this.index++)
    {
        ch=rawsgf.charAt(j);        
        if ( (ch==';') || (ch=='(') || (ch==')') )
        {
            if(sb==0)
            {
                /* console.log(str); */
                break;
            }                            
        }        
        switch (ch)
        {
            case '[':
            {
                sb++;
                if(sb>1)                
                {       
                    str+="{";     
                    sb=1;
                }
                else
                    str+=ch;
                break;
            }            
            case ']':
            {
                if(sb==1)
                    sb--;
                str+=ch;
                break;      
            } 
            case '\\':                
            {
                j++;                
                ch=rawsgf.charAt(j);
                if(ch==']')
                   str+='}';                
                break; 
            }            
            case '\r':
            case '\n':
            case ' ' :
            {
                if(sb==1)
                   str+=ch;                
                break; 
            }  
            default:
            {
                str+=ch;
                break;
            }                       
        }
    }
    return str;
}

Parser.prototype.parseSGF=function(rawsgf)
{
    var index,ch,rb,nodeNumber,start_sgf,begin_sgf,str,nodeInfo;
    
    rb=0;
    nodeNumber=0;
    start_sgf=false;
    begin_sgf=false;
    
    for(index=0;index<rawsgf.length;index++)
    {
        
        ch=rawsgf.charAt(index);
        if(ch=='(') 
            start_sgf=true;
        if(start_sgf && (ch==';'))
            begin_sgf=true;
                        
        if(!begin_sgf)
            continue;
                            
        if(ch==';')
        {
            nodeNumber++;
            this.index=index+1;                        
            str=this.parseSGFNode(rawsgf);
            nodeInfo=this.decodeSGFNode(str);
            this.gametree.addNode(nodeInfo);
            index=this.index-1;            
        }                
        else if(ch=='(')
        {
            rb++;
            this.gametree.pushCurrent();
        }
        else if(ch==')')
        {
             rb--;                     
             this.gametree.popCurrent();
        }                       
    }
    console.log("Parsing sgf done.");    
}


